/**
* @package JCE Media Manager - JCE Flowplayer Extension
* @copyright Copyright (C) 2005 - 2010 Ryan Demmer. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see licence.txt
* This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
*/
WFMediaPlayer.init({	
	params : {
		'extensions':	'flv,mp3,mp4,m4v,xml',
		'controller':	30,
		'dimensions':	{
			'audio'	:	{
				'width' 	: 300, 
				'height' 	: 30
			}
		},
		'path'		:	'http://releases.flowplayer.org/swf/flowplayer-3.2.5.swf',
		'plugins'	:	{
			'audio'		: 	'http://releases.flowplayer.org/swf/flowplayer.audio-3.2.1.swf',
			'controls'	:	'http://releases.flowplayer.org/swf/flowplayer.controls-3.2.3.swf',
			'pseudo'	:	'http://releases.flowplayer.org/swf/flowplayer.pseudostreaming-3.2.5.swf',
			'rtmp'		:	'http://releases.flowplayer.org/swf/flowplayer.rtmp-3.2.3.swf'	
		}
	},
	
	props : {
		'accelerated'			: false,
		'autoPlay' 				: true,
		'autoBuffering' 		: false,
		'bufferLength' 			: 3,
		'baseUrl'				: '',
		'provider'				: '',
		'connectionProvider'	: '',
		'cuepointMultiplier'	: 1000,
		'controls'				: '',
		'cuepoints'				: '',
		'duration'				: 0,
		'extension'				: '',
		'fadeInSpeed'			: 1000,
		'fadeOutSpeed'			: 1000,
		'image'					: false,
		'linkUrl'				: '',
		'linkWindow'			: '_self',
		'live'					: false,
		'metaData'				: '',
		'scaling'				: 'scale',
		'start'					: 0
	},

	isSupported : function(o) {
		var r, url = '', ext = tinymce.explode(this.getParam('extensions')).join('|'), re = new RegExp('\.(' + ext + ')$');
		
		var src 		= o.src || o.data || '';
		var flashvars 	= o.flashvars || '';

		if (flashvars) {
			var data = this.parseValues(flashvars);
			
			if (data) {
				url = data.src || ''; 
			}
		}
		
		// check src and flashvars file for format
		r = re.test(src) || re.test(url);
		
		if (!r) {
			// check src for player file
			return new RegExp(this.getPath()).test(src);
		}
		
		return r;
	},

	getPluginPath : function(s) {
		var plugins = this.getParam('plugins');
		
		return plugins[s] || '';
	},
	
	parseValues : function(s) {
		var o = tinymce.util.JSON.parse(s), data = {src : ''};
		
		if (o && o.clip) {
			data.src = o.clip.url || '';
		}
		return data;
	},
	
	getValues : function(s){
		var t = this, fv = {};

		var config	= {
			clip 	: {
				url		: s,
				baseUrl : encodeURI(tinyMCEPopup.getParam('document_base_url'))
			}
		};
		
		tinymce.each(this.props, function(v, k){
			var n = dom.get('flowplayer_' + k);
			
			if (n) {
				if (n.type == 'checkbox') {
					nv = n.checked ? true : false;
				} else {
					nv = dom.value(n);
				}
				
				if (nv == '' || nv === v) {
					return;
				} else {
					config.clip[k] = nv;
				}
			}
		});
		
		var plugins 	= this.getParam('plugins');		
		var provider 	= dom.value('provider');
		
		if (provider) {
			var plugin 	= dom.value('providerPlugin');	
			var path 	= this.getPluginPath(plugin);
			
			if (path) {
				config['plugins'][plugin]['url'] = path;
			}
		}
		
		var flashvars = dom.value('flash_flashvars');
		
		if (flashvars) {
			fv = MediaManagerDialog.serialize(fv);
		}
		
		var data = 'config=' + MediaManagerDialog.serialize(tinymce.extend(config, fv));
		
		return data.replace(/"/g, "'");
	},
	
	setValues : function(s) {
		var o = tinymce.util.JSON.parse(s);
		
		function setAttribs(v, k) {			
			if (tinymce.is(v, 'string')) {
				switch(k) {
					case 'url':
						dom.value('src', tinyMCEPopup.editor.convertURL(v));
						break;
					case 'baseUrl':
						if (decodeURI(v) != tinyMCEPopup.getParam('document_base_url')) {
							dom.value('flowplayer_' + k, v);
						}
						break;
					default:
						dom.value('flowplayer_' + k, v);
						break;
				}
			} else {
				tinymce.each(v, function(v, k) {
					setAttribs(v, k);
					
					delete o[k];
				});
			}
		}

		setAttribs(o);
		
		var x, fv = '';
		
		for (n in o) {
			x = true;
		}
		if (x) {
			fv = MediaManagerDialog.serialize(o);
		}
		dom.value('flash_flashvars', fv);
	},
	
	/**
	 * Set Dimensions for MediaPlayer if necessary
	 */
	onSetDimensions : function() {
		var src = dom.value('src'), v, k;
		
		if (/\.mp3$/.test(src)) {
			var dimensions = this.getParam('dimensions');
			
			tinymce.each(['width', 'height'], function(k) {
				v = dimensions.audio[k];
				dom.value(k, v);
				dom.value('tmp_' + k, v);
			});
		}
	}
});